package com.graduation.mathematics.ui.activity;

import android.content.Intent;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.graduation.mathematics.R;
import com.graduation.mathematics.base.BaseActivity;
import com.graduation.mathematics.config.Session;
import com.graduation.mathematics.ui.fragment.student.HistoryFragment;
import com.graduation.mathematics.ui.fragment.student.ProblemSetFragment;
import com.graduation.mathematics.ui.fragment.student.SuggestFragment;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import butterknife.BindView;

/**
 * @ProjectName: mathematicsWorkbook
 * @Package: com.graduation.mathematics.ui.activity
 * @ClassName: StudentActivity
 * @Description: java类作用描述
 * @Author: APC
 * @CreateDate: 2021/2/23 15:52
 * @Version: 1.0
 */
public class StudentActivity extends BaseActivity {

    @BindView(R.id.rl_main)
    RelativeLayout mainRl;
    private TextView tvTitle;
    private ImageView imgBack;

    private RadioGroup mTabRadioGroup;
    private SparseArray<Fragment> mFragmentSparseArray;

    @Override
    protected int getLayout() {
        return R.layout.activity_student;
    }

    @Override
    protected void initDataAndEvent() {
        initView();

        mFragmentSparseArray = new SparseArray<>();
        mFragmentSparseArray.append(R.id.tab_problem_sets, ProblemSetFragment.newInstance());
        mFragmentSparseArray.append(R.id.tab_history, HistoryFragment.newInstance());
        mFragmentSparseArray.append(R.id.tab_study_suggest, SuggestFragment.newInstance());
        mTabRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            // 具体的fragment切换逻辑可以根据应用调整，例如使用show()/hide()
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    mFragmentSparseArray.get(checkedId)).commit();
            tvTitle.setText(((RadioButton)findView(checkedId)).getText().toString());
        });
        // 默认显示第一个
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
                mFragmentSparseArray.get(R.id.tab_problem_sets)).commit();
        tvTitle.setText(((RadioButton)findView(R.id.tab_problem_sets)).getText().toString());

    }

    private void initView(){

        if (Session.getScUserInfo().getRileId() == 0) {
            mainRl.setBackground(getResources().getDrawable(R.mipmap.student_bg));
        } else {
            mainRl.setBackground(getResources().getDrawable(R.mipmap.parents_bg));
        }
        tvTitle = findView(R.id.tv_title);
        imgBack = findView(R.id.img_back);
        mTabRadioGroup = findView(R.id.tabs_rg);

        imgBack.setOnClickListener(v -> showBackDialog());
    }

    private void showBackDialog(){
        new XPopup.Builder(this)
                .asConfirm("确认退出登录？", "", "取消", "确认", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        startActivity(new Intent(StudentActivity.this, LoginActivity.class));
                        StudentActivity.this.finish();
                    }
                }, null, false).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
